import { useCallback, } from "react"
import { useSelector, useDispatch } from "react-redux";
import Creators from "../store/actions";

// 引入秒杀api
import secondKillApi from "../api/secondKill"

export const useSecondKill = () => {
    const dispatch = useDispatch();
    const secondKill = useSelector(state => state.secondKill);
    const { eachPage, curPage } = secondKill;

    //请求秒杀商品
    const getSecondKill = useCallback(async () => {
        const data = await secondKillApi.get({ eachPage, curPage });
        dispatch(Creators.skGetSecondkill(data))//SK_GET_SECONDKILL
    }, [eachPage, curPage, dispatch]);



    const setCurPage = useCallback(
        curPage => {
            dispatch(Creators.skSetCurPage({ curPage }))
        }, [dispatch]
    )

    const setEachPage = useCallback(
        eachPage => {
            dispatch(Creators.skSetEachPage({ eachPage }))
        }, [dispatch]
    )

    //新增秒杀商品
    const addSecKill = useCallback(async ({ _id, count, Sale, Time }) => {
        console.log({ _id, count, Sale, Time });
        const data = await secondKillApi.add({ count, Sale, Time, CommId: _id })
        return data
    }, [])


    //修改秒杀商品


    //移出秒杀商品
    const delSecKill = useCallback(async ({ _id}) => {
        console.log({ _id});
        const data = await secondKillApi.delete({ _id })
        return data
    }, [])



    return [secondKill, { getSecondKill, setCurPage, setEachPage, addSecKill,delSecKill }]
}